﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mForum.Web.Common.Infrastructure.AssetManager
{
    using mForum.Web.Common.Infrastructure.Cache;

    public static class PostprocessingCacheHelper
    {
        public const string PostproccessingAssetCacheKey = "PostproccessingAssetCache_{0}_{1}";
        public const string AssetListCacheKey = "Assets_{0}_{1}";

        public static object SyncRoot = new object();

        private static PostprocessingCacheItem EnsureItem(string key, ICache cache)
        {
            PostprocessingCacheItem item = cache.Get<PostprocessingCacheItem>(key);

            if (item == null)
            {
                DateTime expiresOn = DateTime.Now.AddHours(1);
                item = new PostprocessingCacheItem();

                cache.Set<PostprocessingCacheItem>(key, item, expiresOn);
            }

            return item;
        }

        public static string GetContent(string key, ICache cache)
        {
            PostprocessingCacheItem item = EnsureItem(key, cache);
            return item.Content;
        }

        public static void SetContent(string key, string content, ICache cache)
        {
            PostprocessingCacheItem item = EnsureItem(key, cache);
            item.Content = content;
        }

        public static DateTime GetLastModificationDate(string key, ICache cache)
        {
            PostprocessingCacheItem item = EnsureItem(key, cache);
            return item.LastModificationDate;
        }

        public static List<AssetItem> GetFilesList(string assetType, string assetCategory, ICache cache)
        {
            return cache.Get<List<AssetItem>>(String.Format(AssetListCacheKey, assetType, assetCategory));
        }

        public static string CreatePostprocessingCacheKey(string assetType, string assetCategory)
        {
            return String.Format(PostproccessingAssetCacheKey, assetType, assetCategory);
        }
    }
}
